/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.proc.base.rule;

import org.ipig.proc.cache.CachedUnit;
import org.ipig.proc.core.Action;
import org.ipig.proc.core.Collector;
import org.ipig.proc.core.Filter;
import org.ipig.proc.core.Key;
import org.ipig.proc.core.Model;
import org.ipig.proc.core.Variable;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 规则Unit
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: RuleUnit.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
public interface RuleUnit extends Callable<Collection<CachedUnit>> {
    //数据存储的期限，默认7天[单位Millisecond]，为0表示永不过期【TTL】
    public static final Long DURATION_TIME = 604800000L;

    //获取规则名称
    public abstract String getRuleName();

    //获取度量指标名称
    public abstract String getMetrics();

    public abstract Boolean getIgnorePattern();

    public abstract void setIgnorePattern(Boolean ignorePattern);

    /**
     * 将模型数据放入逻辑计算单元里
     *
     * @param modelUnitMap Map<Class<?>, List<? extends ModelUnit>>
     */
    public abstract void setModels(Map<String, List<? extends Model>> modelUnitMap);

    public abstract Map<String, List<? extends Model>> getModels();

    /**
     * 执行
     *
     * @param map
     * @return
     * @throws InterruptedException
     */
    public abstract Collection<CachedUnit> execute(Map<String, List<? extends Model>> map) throws InterruptedException;

    public abstract RuleUnit clone();

    @Override
    public default Collection<CachedUnit> call() throws InterruptedException {
        return execute(this.getModels());
    }

    /**
     * 新建RuleUnit
     *
     * @param name          规则名称
     * @param biz           业务
     * @param dim           维度
     * @param key           模型唯一标识
     * @param action        行为过程
     * @param metrics       度量指标名称
     * @param collector     收集器
     * @param expirePattern 过期模式
     * @param timeAxis      时间轴
     * @param lazyGroup
     * @param selector      模型选择器
     * @param filters       条件过滤器
     * @param duration      数据存储的期限
     * @return
     */
    public RuleUnit newInstance(String name, String biz, String dim, Key key, Action action, String metrics,
                                Collector collector, Object expirePattern, Variable timeAxis, Boolean lazyGroup,
                                Class<?> selector, List<Filter> filters, Long duration);
}